local Lang = require("common.Lang")
local Human = require("core.Human")
local Msg = require("core.Msg")
local TeamLogic = require("team.TeamLogic")
local Broadcast = require("broadcast.Broadcast")
local VipLevel = require("vip.VipLevel")
local os = _G.os

function LW_TEAM_CHAT(human, msg)
    TeamLogic.sendMsg2Member(human, msg)
end

function CG_TEAM_LIST_QUERY(human)
	TeamLogic.sendList(human)
end

function CG_TEAM_INVITE(invitor,msg)
	local targetUuid = msg.targetUuid
	local target = Human.onlineUuid[targetUuid]

	if not check(invitor._id,targetUuid,true) then
		return
	end

	local targetTeam = TeamLogic.getTeamByUuid(target._id)
	
	local msgRet = Msg.gc.GC_TEAM_INVITE_NOTIFY
	msgRet.invitorName = invitor.name
	msgRet.invitorUuid = invitor._id
	msgRet.invitorID = invitor.id	
	msgRet.vipLv = VipLevel.getVipLv(invitor.db)
	msgRet.lv = invitor.db.lv
	msgRet.sex = invitor.db.sex
	
	msgRet.targetName = target.name
	msgRet.targetUuid = target._id
	msgRet.targetID = target.id
	
	if targetTeam then
		-- 组队邀请要发给队长
		local teamLeader = Human.onlineUuid[TeamLogic.getLeaderUuid(targetTeam)]
		if teamLeader then		
			if false then	--自动入队
				doTeamInviteNotifyRet(invitor.db._id,targetUuid, 1)
				return true
			end
			Msg.send(msgRet,teamLeader.fd)
		end
	else
		Broadcast.sendBroadcast(invitor,Lang.DOWN,Lang.TEAM_WAIT_TARGET_AGREE)
		Msg.send(msgRet, target.fd)
	end
	
	invitor.teamInvite[targetUuid] = os.time()
end

function check(invitorUuid,targetUuid,checkDoublcInvite)
	
	local invitor = Human.onlineUuid[invitorUuid]
	local target = Human.onlineUuid[targetUuid]

	-- check invitor 
	if not invitor then
		return false
	end

	-- check target 
	if not target then
		Broadcast.sendBroadcast(invitor,Lang.DOWN,Lang.TEAM_INVALID_ID)
		return false
	end

	-- check double invaite
	local preTime = invitor.teamInvite[targetUuid]
	local now = os.time()
	if preTime and now - preTime < 60 and checkDoublcInvite then
		Broadcast.sendBroadcast(invitor,Lang.DOWN,Lang.TEAM_DUPLICATE_INVAITE)
		return false
	end
	if invitor.id == target.id then
		Broadcast.sendBroadcast(invitor,Lang.DOWN,Lang.TEAM_NOT_INVITE_YOUSELF)
		return false
	end

	local invitorTeam = TeamLogic.getTeamByUuid(invitor._id)
	local targetTeam = TeamLogic.getTeamByUuid(target._id)

	--  邀请人和被邀请人都有不同队伍了
	if invitorTeam and targetTeam and invitorTeam.id ~= targetTeam.id then	
		Broadcast.sendBroadcast(invitor,Lang.DOWN,Lang.TEAM_BOTH_HAVE_TEAM)
		return false
	end

	-- 已经在同一个队伍了
	if invitorTeam and targetTeam and invitorTeam.id == targetTeam.id then
		invitor.teamInvite[target._id] = nil
		Broadcast.sendBroadcast(invitor,Lang.DOWN,Lang.TEAM_IN_SAME_TEAM)
		return false
	end

	-- 邀请人队伍已满
	if invitorTeam and invitorTeam.count >= TeamLogic.TEAM_MAX_MEMBER_COUNT then 
		Broadcast.sendBroadcast(invitor,Lang.DOWN,Lang.TEAM_SRC_TEAM_FULL)
		return false
	end

	-- 被邀请人队伍已满
	if targetTeam and targetTeam.count >= TeamLogic.TEAM_MAX_MEMBER_COUNT then 
		Broadcast.sendBroadcast(invitor,Lang.DOWN,Lang.TEAM_TAR_TEAM_FULL)
		return false
	end

	return true
end

function doTeamInviteNotifyRet(invitorUuid, targetUuid, result)

	local invitor = Human.onlineUuid[invitorUuid]
	local target = Human.onlineUuid[targetUuid]
	
	if not invitor or not target then
		return
	end
	
	invitor.teamInvite[targetUuid] = nil
	
	local invitorTeam = TeamLogic.getTeamByUuid(invitorUuid)
	local targetTeam = TeamLogic.getTeamByUuid(targetUuid)
	
	if result ~= 1 then
		Broadcast.sendBroadcast(invitor,Lang.DOWN,target.name .. Lang.TEAM_INVITE_FAIL_REFUSE)
		return true		
	end
		
	local newTeam = nil
	local newComer = nil
	if not invitorTeam and not targetTeam then
		newTeam = TeamLogic.create(invitor)
		newComer = target
		TeamLogic.addMember(newTeam, target)
		Broadcast.sendBroadcast(invitor,Lang.DOWN,target.name .. Lang.TEAM_ENTER_OK)
		Broadcast.sendBroadcast(target,Lang.DOWN,Lang.TEAM_JOIN_OK)
	elseif not invitorTeam then
		local fds = TeamLogic.getTeamOnlineMemberFD(targetTeam)
		Broadcast.sendBroadcastFDs(fds,Lang.DOWN,invitor.name .. Lang.TEAM_ENTER_OK)
		Broadcast.sendBroadcast(invitor,Lang.DOWN,Lang.TEAM_JOIN_OK)
		TeamLogic.addMember(targetTeam, invitor)
		newTeam = targetTeam
		newComer = invitor
	elseif not targetTeam then
		local fds = TeamLogic.getTeamOnlineMemberFD(invitorTeam)
		Broadcast.sendBroadcastFDs(fds,Lang.DOWN,target.name .. Lang.TEAM_ENTER_OK)
		Broadcast.sendBroadcast(target,Lang.DOWN,Lang.TEAM_JOIN_OK) 
		
		TeamLogic.addMember(invitorTeam, target)
		newTeam = invitorTeam
		newComer = target
	end
	
	-- 刷新队伍成员信息
	TeamLogic.sendLists(newTeam)
end

function CG_TEAM_INVITE_NOTIFY_RET(human,msg)
	local invitorUuid = msg.invitorUuid
	local targetUuid = msg.targetUuid
	if not check(invitorUuid,targetUuid) then
		return
	end
	doTeamInviteNotifyRet(invitorUuid, targetUuid, msg.result)
end

function CG_TEAM_EXIT(human)
	TeamLogic.exit(human._id)
end

function CG_TEAM_CREATE(human)
	if TeamLogic.getTeamByUuid(human._id) == nil then
		TeamLogic.create(human)
		TeamLogic.sendList(human)
	end
end

function CG_TEAM_DISMISS(human)
	TeamLogic.dismiss(human._id)
end

function CG_TEAM_MEMBER_KICK(human,msg)
	TeamLogic.kick(human,msg.name, msg.uuid)
end

function CG_TEAM_LEADER_CHANGE(human,msg)
	TeamLogic.changeLeader(human,msg.targetUuid)
end

function CG_TEAM_BUFF_QUERY(human)
	local team = TeamLogic.getTeamByUuid(human._id)
	if not team then
		return
	end
	local msgRet = Msg.gc.GC_TEAM_BUFF_QUERY
	local roleExpRate,roleLvRate = ExpLogic.getTeamRate(human)
	msgRet.roleExpRate = roleExpRate
	msgRet.roleLvRate = roleLvRate
	Msg.send(msgRet,human.fd)
end


function CG_TEAM_GET_SCENE(human)
  TeamLogic.CG_TEAM_GET_SCENE(human)
end